import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

// This is a servlet called when user wants to unsubscribe.
public class User_unsubscribe extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
 response.setContentType("text/html");
 PrintWriter pw = response.getWriter();
 String applicationid = request.getParameter("applicationid");
 String applicationcredentials = request.getParameter("applicationcredentials");
 String login = request.getParameter("login");
 
 //connect to the database
 Connection con = null;
 PreparedStatement pst = null;
 PreparedStatement pst2 = null;
 ResultSet rs = null;
 String query = "";
 String query2 = "";
 try {
  Class.forName("com.mysql.jdbc.Driver").newInstance();
 con = DriverManager.getConnection("jdbc:mysql://bungle08.cs.toronto.edu:3306/group22",
   "group22", "123456");
   // Remove user from the user table and course_enrolled table
 
   query = "DELETE FROM user WHERE id =\""+login+"\"";
   query2 = "DELETE FROM course_enrolled WHERE user_name =\""+login+"\"";  
   pst = con.prepareStatement(query);  
   pst2 = con.prepareStatement(query2);
   int i = pst.executeUpdate();
   int j = pst2.executeUpdate();
   if(i!=0 && j !=0){
    
   }  
   pw.println("{\"status\":200}");  
 }
  catch(Exception e) {
  pw.println("Exception: " + e.getMessage());
 } finally {
  try {
   if(con != null)
    con.close();
  } catch(SQLException e) {}
 
 }
 }
}